<template>
  <div class="drag1_container">
    <transition-group>
      <li
        v-for="(item,index) in todolist"
        :key="item.label"
        draggable
        :class="['list-item', { 'is-dragover': index === newIndex }]"
        @dragover.prevent="dragOver(index)"
        @dragstart="dragStart(item,index)"
        @dragend="dragEnd"
      >{{item.label}}</li>
    </transition-group>
  </div>
</template>
<script>
export default {
  name: 'drag1_container',
  data() {
    return {
      todolist: [
        { label: '列表1' },
        { label: '列表2' },
        { label: '列表3' },
        { label: '列表4' },
        { label: '列表5' },
        { label: '列表6' },
      ],
      oldIndex: '',
      oldData: '',
      newIndex: '',
    }
  },
  mounted() {},
  methods: {
    // 开始拖拽
    dragStart(item, index) {
      this.oldIndex = index
      this.oldData = item
    },
    // 拖动中
    dragOver(index) {
      this.newIndex = index
    },
    // 拖动结束
    dragEnd() {
      let newItems = [...this.todolist]
      // 删除老的节点
      newItems.splice(this.oldIndex, 1)
      // 在列表中目标位置增加新的节点
      newItems.splice(this.newIndex, 0, this.oldData)
      this.todolist = [...newItems]
      this.newIndex = ''
    },
  },
}
</script>
<style lang="less" scoped>
.drag1_container {
  list-style: none;
  .list-item {
    position: relative;
    cursor: move;
    width: 300px;
    background: #ea6e59;
    border-radius: 4px;
    color: #fff;
    margin: 10px 0;
    height: 50px;
    line-height: 50px;
    text-align: center;
  }
  .list-item.is-dragover::before {
    content: '';
    position: absolute;
    bottom: -8px;
    left: 0;
    width: 100%;
    height: 4px;
    background-color: #0c6bc9;
  }
  .list-item.is-dragover::after {
    content: '';
    position: absolute;
    bottom: -12px;
    left: -6px;
    border: 3px solid #0c6bc9;
    border-radius: 50%;
    width: 6px;
    height: 6px;
    background-color: #fff;
  }
}
</style>